<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2015/8/10
 * Time: 11:02
 */
namespace Home\Controller;

use Think\Controller;

/**
 * 班次搜索控制器
 * Class SearchController
 * @package Home\Controller
 */
class SearchController extends Controller
{
    //获取站点的接口参数
    const TYPE_ALL = 0;//所有
    const TYPE_CHAR = 1;//首字母
    const TYPE_AREA = 2;//地区
    const TYPE_KEYWORD = 3;//关键词
    const TYPE_HOT = 9;//热门站点

    /**
     * 搜索
     */
    public function search()
    {
        $data = $this -> startStation();
        $startStationCode = $data['data']['defaultStation']['station_code'];
        $post_string = "cmd=GetNode&args={param:{stationCode:'".$startStationCode."',searchType:'" . self::TYPE_HOT . "',key1:''}}";
        $temp = json_decode(post(C('REMOTE_INTERFACE_BASE_URL'), $post_string), true);
        $data['hotStation'] = $temp['data']['list'];
        //dump($data['hotStation']);die;
        $this ->assign("json",$data);
        $this->display();
    }

    /**
     * 班次
     */
    public function schedule()
    {
        $startStation = I('start-station');
        $endStation = I('end-station');
        $date = I('date');//日期
        $endStationCode = I("end-station-code");//
        $startStationCode = I("start-station-code");
        $post_string = "cmd=GetSchemeList&args={param:{stationCode:'".$startStationCode."',nodeCode:'" . $endStationCode . "',schDate:'" . $date . "'}}";
        $json = json_decode(post(C('REMOTE_INTERFACE_BASE_URL'), $post_string), true);
        $json['startStationCode'] = $startStationCode;
        $json['endStationCode'] = $endStationCode;
        $json['date'] = $date;
        //exit(json_encode($json));
        $this->assign("json", $json);
        $this->display();
    }

    /**
     * 上一天下一天 ajax
     */
    public function ajaxSche(){
        $startStationCode = I("startStationCode");
        $endStationCode = I("endStationCode");
        $date = I("date");
        $post_string = "cmd=GetSchemeList&args={param:{stationCode:'".$startStationCode."',nodeCode:'" . $endStationCode . "',schDate:'" . $date . "'}}";
        exit(post(C('REMOTE_INTERFACE_BASE_URL'),$post_string));
    }

    /**
     * 获取省份 ajax
     */
    public function province()
    {
        $startStationCode = I("stationCode");
        $post_string = "cmd=GetNodeProvince&args={param:{stationCode:'".$startStationCode."'}}";
        exit(post(C('REMOTE_INTERFACE_BASE_URL'), $post_string));
    }

    /**
     * 获取城市 ajax
     */
    public function city()
    {
        $startStationCode = I("stationCode");
        $province_code = I("province_code");
        $post_string = "cmd=GetNodeCity&args={param:{stationCode:'".$startStationCode."',province_code:'" . $province_code . "'}}";
        exit(post(C('REMOTE_INTERFACE_BASE_URL'), $post_string));
    }

    /**
     * ajax
     */
    public function station()
    {
        $startStationCode = I("stationCode");
        $province_code = I("province_code");
        $city_code = I("city_code");
        $post_string = "cmd=GetNode&args={param:{stationCode:'".$startStationCode."',searchType:'" . self::TYPE_AREA . "',key1:'" . $province_code . "',key2:'" . $city_code . "'}}";
        exit(post(C('REMOTE_INTERFACE_BASE_URL'), $post_string));
    }

    /**获得所有起始站
     * @return mixed
     */
    public function startStation()
    {
        $post_string = "cmd=GetStationList&args={}";
        $json = json_decode(post(C('REMOTE_INTERFACE_BASE_URL'), $post_string), true);
        if (count($json['data']['list']) > 0) {
            $json['data']['defaultStation'] = $json['data']['list'][0];
        }
        return $json;
    }

    /**
     * 根据字母获得站点s ajax
     */
    public function char()
    {
        $startStationCode = I("stationCode");
        $char = I("char");
        $post_string = "cmd=GetNode&args={param:{stationCode:'".$startStationCode."',searchType:'" . self::TYPE_CHAR . "',key1:'" . $char . "'}}";
        exit(post(C('REMOTE_INTERFACE_BASE_URL'), $post_string));
    }

    /**
     * 获得热门站点 ajax
     */
    public function hot()
    {
        $startStationCode = I("stationCode");
        $post_string = "cmd=GetNode&args={param:{stationCode:'".$startStationCode."',searchType:'" . self::TYPE_HOT . "',key1:''}}";
        exit(post(C('REMOTE_INTERFACE_BASE_URL'), $post_string));
    }

    /**
     * 查询站点 ajax
     */
    public function doSearch()
    {
        $startStationCode = I("stationCode");
        $keyword = I("keyword");
        $post_string = "cmd=GetNode&args={param:{stationCode:'".$startStationCode."',searchType:'" . self::TYPE_KEYWORD . "',key1:'" . $keyword . "'}}";
        exit(post(C('REMOTE_INTERFACE_BASE_URL'), $post_string));
    }
}